﻿using System;
using VirtualTournaments.Business.Users;
using VirtualTournaments.Business.Financial;

namespace VirtualTournaments.Business.Bets
{

    public class Bet
    {

		public Bet(User bettor, decimal wageredMoney, decimal odds)
        {
            this.Bettor = bettor;
			this.WageredMoney = wageredMoney;
            this.Odds = odds;
        }
        /// <summary>
        /// El apostador
        /// </summary>
        public virtual User Bettor { get; protected set; }

        /// <summary>
        /// La cantidad de dinero apostado
        /// </summary>
		public decimal WageredMoney { get; set; }

        /// <summary>
        /// Las cuotas en el momento de la apuesta
        /// </summary>
        public decimal Odds { get; set; }
        
        /// <summary>
        /// Si la apuesta fué liquidada
        /// </summary>
        private bool settled = false;

        private decimal GetPayment()
        {
            return this.WageredMoney * this.Odds;            
        }

		/// <summary>
		/// Paga el valor correspondiente con una retencion porcentual
		/// </summary>
		/// <param name="accountFrom"></param>
		/// <param name="holdback">el porcentaje que no se transpaza, porque se retiene en la cuenta desde</param>
		internal void Pay(IAccount accountFrom, int holdback)
        {
            if (!settled)
            {
				decimal payment = this.GetPayment() * (100 - holdback) / 100;
                accountFrom.transfer(Bettor.Account, payment);
            }
            settled = true;
        }
    }
}
